import { Language } from "../../../../../extend/language/Language";
import { BaseItem } from "../../common/BaseItem";
import { ArrayUtil } from "../../../../../com/gengine/utils/ArrayUtil";
import { BgItem } from "../../common/BgItem";
import { ItemData } from "../../../resource/info/ItemData";
import { Cache } from "../../../cache/Cache";
import { MsgManager } from "../../../manager/MsgManager";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { EGuildPosition } from "../../../../../Message/Game/EGuildPosition";
import { AssignGuildWarehouseExVo } from "./vo/AssignGuildWarehouseExVo";
import { EGuildAssignType } from "../../../../../Message/Game/EGuildAssignType";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { GuildUtil } from "../info/GuildUtil";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { LayerManager } from "../../../manager/LayerManager";
import { GameController } from "../../../mvc/GameController";
import { SGuildWarehouseApply } from "../../../../../Message/Game/SGuildWarehouseApply";
import { SGuildPlayerAttendanceVo } from "./vo/SGuildPlayerAttendanceVo";
import { AllocationConfirmWindow } from "./AllocationConfirmWindow";
import { ReplyListWindow } from "../warehouse/ReplyListWindow";
import { TakeOutMoneyWindow } from "../warehouse/TakeOutMoneyWindow";
import { TextBox } from "../../pack/TextBox";
import { ObjCreate } from "../../common/ObjCreate";
import { GTabBar } from "../../../../../com/mui/controls/GTabBar";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { MuiEvent } from "../../../../../com/mui/events/MuiEvent";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { WarehouseGoodsItemRenderer } from "./itemRenderer/WarehouseGoodsItemRenderer";
import { ListEvent } from "../../../../../fl/events/ListEvent";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { DragEvent } from "../../../../../com/mui/events/DragEvent";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
type int = number;
//class SingleAllocationView
    
    export  class SingleAllocationView /* flash.display.Sprite */
    {
        constructor()
        {
            this._itemsData = [];
            this._allocationGoodsData = [];
            this._tabData = [{"name":"all", "label": Language.getString(57295)}, {"name":"prop", "label": Language.getString(57296)}, {"name":"equipment", "label": Language.getString(57297)}, {"name":"jewel", "label": Language.getString(57298)}];
            
            this.init();
            return;
        }

        /* internal  */onPutDownItem(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as BaseItem;
            if (loc1 && loc1.itemData) 
            {
                ArrayUtil.removeItem(this._allocationGoodsData, loc1.itemData);
                this.allocationItems = this._allocationGoodsData;
            }
            return;
        }

        /* internal  */set allocationItems(arg1: Array<any>)
        {
            var loc1=null;
            var loc2=null;
            var loc4=0;
            var loc3=arg1.length;
            loc4 = 0;
            while (loc4 < 5) 
            {
                loc2 = this._bgItemSp.getChildAt(loc4) as BgItem;
                loc2.setItemCode(0);
                ++loc4;
            }
            loc4 = 0;
            while (loc4 < loc3) 
            {
                loc1 = arg1[loc4] as ItemData;
                loc2 = this._bgItemSp.getChildAt(loc4) as BgItem;
                loc2.itemData = loc1;
                ++loc4;
            }
            return;
        }

        /* internal  */onBottomBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=0;
            var loc4=null;
            if (!Cache.instance.guild.isCanManageWarehouse()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27973));
                return;
            }
            if (this._curSelectMemberData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27974));
                return;
            }
            var loc5=arg1.target.name;
            switch (loc5) 
            {
                case "inputMoneyBtn":
                {
                    this.takeOutMoneyWindow.show();
                    this.takeOutMoneyWindow.title = Language.getString(27970);
                    this.takeOutMoneyWindow.callBackFun = this.callBackFun;
                    this.takeOutMoneyWindow.maxGold = this._storageGold;
                    this.takeOutMoneyWindow.maxCoin = this._storageCoin;
                    this.setTakeOutMoneyWindowStorageMoney(this._storageGold, this._storageCoin);
                    break;
                }
                case "allowAppleBtn":
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseGetAllApply));
                    break;
                }
                case "confirmBtn":
                {
                    if (this._curSelectMemberData && this._curSelectMemberData.itemData.position == EGuildPosition._EGuildLeader) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(27984));
                        return;
                    }
                    loc1 = this._allocationGoodsData.length;
                    if (loc1 > 0 || this.goldOutText > 0 || this.coinOutText > 0) 
                    {
                        loc2 = new AssignGuildWarehouseExVo();
                        loc2.type = EGuildAssignType.convert(EGuildAssignType._EGuildAssignSingle);
                        loc3 = 0;
                        while (loc3 < loc1) 
                        {
                            if (loc4 = this._allocationGoodsData[loc3] as ItemData) 
                            {
                                loc2.itemMap[loc4.uid] = loc4.itemAmount;
                            }
                            ++loc3;
                        }
                        if (this._curSelectMemberData) 
                        {
                            loc2.toPlayerIds.push(this._curSelectMemberData.itemData.miniPlayer.entityId.id);
                        }
                        loc2.moneyMap[EPrictUnit._EPriceUnitGold] = this.goldOutText;
                        loc2.moneyMap[EPrictUnit._EPriceUnitCoin] = this.coinOutText;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ConfirmAssignGuildWarehouseEx, {"vo": loc2}));
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */callBackFun(arg1: Boolean, arg2: int, arg3: int): void
        {
            if (arg2 > 0 && arg2 <= this._storageGold) 
            {
                this.goldOutText = arg2;
            }
            else if (arg3 > 0 && arg3 <= this._storageCoin) 
            {
                this.coinOutText = arg3;
            }
            return;
        }

        /* internal  */set coinOutText(arg1: int)
        {
            this._coinOutText.text = arg1.toString();
            return;
        }

        public setGuildWarehouseItems(arg1: Array<any>): void
        {
            GuildUtil.tabIndex = this._tabBar.selectedIndex;
            this._itemsData = arg1;
            var loc1=this._itemsData.filter(GuildUtil.onFilterItem);
            this._goodsList.dataProvider = new DataProvider(loc1);
            this._goodsList.drawNow();
            return;
        }

        public setGuildWarehouseMoney(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            if (arg1) 
            {
                this._storageGold = arg1[EPrictUnit._EPriceUnitGold];
                this._storageCoin = arg1[EPrictUnit._EPriceUnitCoin];
            }
            this.setStorageMoneyAmount(this._storageGold, this._storageCoin);
            this.setTakeOutMoneyWindowStorageMoney(this._storageGold, this._storageCoin);
            return;
        }

        public setGuildWarehouseReplyList(arg1: Array<any>): void
        {
            var loc2=null;
            var loc1=[];
            var loc3=0;
            var loc4=arg1;
            for(loc2 of loc4) 
            {
                if (loc2.playerId != this._curSelectMemberData.itemData.miniPlayer.entityId.id) 
                {
                    continue;
                }
                loc1.push(loc2);
            }
            this.replyListWindow.setDefaultPage(loc1, true);
            if (this.replyListWindow.isHide) 
            {
                if (Cache.instance.country.isOpenCountry) 
                {
                    LayerManager.windowLayer.tweenCenterWindow(GameController.countryPosition.socialModule, this.replyListWindow);
                }
                else 
                {
                    LayerManager.windowLayer.tweenCenterWindow(GameController.guild.view, this.replyListWindow);
                }
            }
            return;
        }

        public removeReplyedApply(arg1: SGuildWarehouseApply): void
        {
            if (this._replyListWindow && !this._replyListWindow.isHide) 
            {
                this.replyListWindow.removeReplyedApply(arg1);
            }
            return;
        }

        public set curSelectMemberData(arg1: SGuildPlayerAttendanceVo)
        {
            this._curSelectMemberData = arg1;
            return;
        }

        /* internal  */setStorageMoneyAmount(arg1: int, arg2: int): void
        {
            this._goldTextBox.textField.text = arg1.toString();
            this._coinTextBox.textField.text = arg2.toString();
            return;
        }

        /* internal  */get allocationConfirmWindow(): AllocationConfirmWindow
        {
            if (this._allocationConfirmWindow == null) 
            {
                this._allocationConfirmWindow = new AllocationConfirmWindow();
            }
            return this._allocationConfirmWindow;
        }

        /* internal  */get replyListWindow(): ReplyListWindow
        {
            if (this._replyListWindow == null) 
            {
                this._replyListWindow = new ReplyListWindow();
            }
            return this._replyListWindow;
        }

        /* internal  */get takeOutMoneyWindow(): TakeOutMoneyWindow
        {
            if (this._takeOutMoneyWindow == null) 
            {
                this._takeOutMoneyWindow = new TakeOutMoneyWindow();
            }
            return this._takeOutMoneyWindow;
        }

        /* internal  */createTextBox(arg1: int, arg2: int): TextBox
        {
            var loc1=new TextBox();
            loc1.textFieldHeight = 18;
            loc1.textFieldWidth = 80;
            loc1.bgHeight = 18;
            loc1.bgWidth = 80;
            this.addChild(loc1);
            return loc1;
        }

        /* internal  */init(): void
        {
            var loc13=null;
            var loc1=ObjCreate.createBg(5, -6, 257, 230, this);
            this._tabBar = new GTabBar();
            this._tabBar.buttonWidth = 50;
            this._tabBar.height = 22;
            this._tabBar.x = 10;
            this._tabBar.y = 0;
            this._tabBar.horizontalGap = 2;
            this._tabBar.buttonFilters = [FilterConst.glowFilter];
            this._tabBar.dataProvider = this._tabData;
            this._tabBar.addEventListener(MuiEvent.GTABBAR_SELECTED_CHANGE, this.onTabBarChangeHandler);
            this.addChild(this._tabBar);
            ObjCreate.createBg(5, 190, 260, 2, this, ImagesConst.SplitLine);
            this._goodsList = ObjCreate.createTileList(8, 30, 253, 160, this);
            this._goodsList.scrollPolicy = ScrollPolicy.AUTO;
            this._goodsList.columnWidth = 39;
            this._goodsList.rowHeight = 40;
            this._goodsList.setStyle("cellRenderer", WarehouseGoodsItemRenderer);
            this._goodsList.addEventListener(ListEvent.ITEM_CLICK, this.onSelectItemHandler);
            ObjCreate.createBg(5, 26, 260, 2, this, ImagesConst.SplitLine);
            var loc2=GlobalClass.getBitmap("Yuanbao");
            ObjCreate.setObjAttri(loc2, 15, 196, -1, -1, this);
            this._goldTextBox = this.createTextBox(0, 0);
            this._goldTextBox.x = 38;
            this._goldTextBox.y = 196;
            var loc3=GlobalClass.getBitmap("Tongqian");
            ObjCreate.setObjAttri(loc3, 150, 196, -1, -1, this);
            this._coinTextBox = this.createTextBox(1, 0);
            this._coinTextBox.x = 170;
            this._coinTextBox.y = 196;
            var loc4=ObjCreate.createBg(5, 228, 257, 141, this);
            var loc5=ObjCreate.createGTextField(Language.getString(27961), 10, 235, 180, 20, this, GlobalStyle.textFormat2, true);
            this._bgItemSp = new Object /* flash.display.Sprite */();
            ObjCreate.setObjAttri(this._bgItemSp, 15, 57, -1, -1, this);
            var loc6=0;
            while (loc6 < 5) 
            {
                loc13 = new BgItem();
                ObjCreate.setObjAttri(loc13, loc6 * 50, 200, -1, -1, this._bgItemSp);
                loc13.doubleClickEnabled = true;
                loc13.baseItem.doubleClickEnabled = true;
                loc13.baseItem.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.onPutDownItem);
                ++loc6;
            }
            var loc7=GlobalClass.getBitmap("Yuanbao");
            ObjCreate.setObjAttri(loc7, 15, 310, -1, -1, this);
            this._goldOutText = ObjCreate.createGTextField("0", 35, 310, 180, 20, this, GlobalStyle.textFormat2);
            var loc8=GlobalClass.getBitmap("Tongqian");
            ObjCreate.setObjAttri(loc8, 150, 310, -1, -1, this);
            this._coinOutText = ObjCreate.createGTextField("0", 170, 310, 180, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createBg(5, 330, 260, 3, this, ImagesConst.SplitLine);
            var loc9;
            (loc9 = ObjCreate.createGButton("清空", 212, 232, 42, 22, this)).addEventListener(flash.events.MouseEvent.CLICK, this.onCleanPro);
            var loc10;
            (loc10 = ObjCreate.createGButton(Language.getString(27964), 16, 337, 72, 28, this)).name = "inputMoneyBtn";
            loc10.addEventListener(flash.events.MouseEvent.CLICK, this.onBottomBtnClickHandler);
            var loc11;
            (loc11 = ObjCreate.createGButton(Language.getString(27965), 98, 337, 72, 28, this)).name = "allowAppleBtn";
            loc11.addEventListener(flash.events.MouseEvent.CLICK, this.onBottomBtnClickHandler);
            var loc12;
            (loc12 = ObjCreate.createGButton(Language.getString(27966), 180, 337, 72, 28, this)).name = "confirmBtn";
            loc12.addEventListener(flash.events.MouseEvent.CLICK, this.onBottomBtnClickHandler);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onCleanHandler);
            Dispatcher.addEventListener(EventName.ApproveGuildWarehouseRecordsRequest, this.onAddItem);
            return;
        }

        public onCleanHandler(arg1: Object /* flash.events.Event */=null): void
        {
            this.goldOutText = 0;
            this.coinOutText = 0;
            if (arg1) 
            {
                this._curSelectMemberData = null;
            }
            this._allocationGoodsData = [];
            this.allocationItems = this._allocationGoodsData;
            return;
        }

        /* internal  */onCleanPro(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (!Cache.instance.guild.isCanManageWarehouse()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27973));
                return;
            }
            this.goldOutText = 0;
            this.coinOutText = 0;
            this._allocationGoodsData = [];
            this.allocationItems = this._allocationGoodsData;
            return;
        }

        /* internal  */onTabBarChangeHandler(arg1: MuiEvent): void
        {
            var loc1=arg1.target.selectedIndex;
            GuildUtil.tabIndex = loc1;
            var loc2=this._itemsData.filter(GuildUtil.onFilterItem);
            this._goodsList.dataProvider = new DataProvider(loc2);
            this._goodsList.drawNow();
            return;
        }

        /* internal  */set goldOutText(arg1: int)
        {
            this._goldOutText.text = arg1.toString();
            return;
        }

        /* internal  */get goldOutText(): int
        {
            return int(this._goldOutText.text);
        }

        /* internal  */setTakeOutMoneyWindowStorageMoney(arg1: int, arg2: int): void
        {
            if (this._takeOutMoneyWindow && !this._takeOutMoneyWindow.isHide) 
            {
                this.takeOutMoneyWindow.storageGold = this._storageGold;
                this.takeOutMoneyWindow.storageCoin = this._storageCoin;
            }
            return;
        }

        /* internal  */get coinOutText(): int
        {
            return int(this._coinOutText.text);
        }

        /* internal  */onMoveInHandler(arg1: DragEvent): void
        {
            var loc1=arg1.dragItem as BaseItem;
            var loc2=arg1.dropItem as BaseItem;
            if (loc1 == null || loc2 == null || ItemsUtil.isBind(loc1.itemData)) 
            {
                return;
            }
            loc2.itemData = loc1.itemData;
            return;
        }

        /* internal  */onSelectItemHandler(arg1: ListEvent): void
        {
            if (!Cache.instance.guild.isCanManageWarehouse()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27973));
                return;
            }
            if (this._allocationGoodsData.length >= 5) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27982));
                return;
            }
            var loc1=arg1.item as ItemData;
            if (loc1 && loc1.itemAmount == 1) 
            {
                this.addItem(loc1);
            }
            else if (loc1 && loc1.itemAmount > 1) 
            {
                this.allocationConfirmWindow.updateByItemData(loc1);
                this.allocationConfirmWindow.responFun(this.addItem);
                this.allocationConfirmWindow.show();
            }
            return;
        }

        /* internal  */addItem(arg1: ItemData): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._allocationGoodsData;
            for(loc1 of loc3) 
            {
                if (loc1.uid != arg1.uid) 
                {
                    continue;
                }
                loc1.itemAmount = arg1.itemAmount;
                this.allocationItems = this._allocationGoodsData;
                return;
            }
            this._allocationGoodsData.push(arg1);
            this.allocationItems = this._allocationGoodsData;
            return;
        }

        /* internal  */onAddItem(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (loc1 && this._allocationGoodsData.length < 5) 
            {
                this.addItem(loc1);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(27982));
            }
            return;
        }

        /* internal  */onAddMoney(arg1: DataEvent): void
        {
            var loc1=arg1.data.unit;
            var loc2=arg1.data.amount;
            if (loc1 == 0 || loc1 == 1) 
            {
                if (this._storageCoin < this.coinOutText + loc2) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(27958));
                }
                else 
                {
                    this.coinOutText = this.coinOutText + loc2;
                }
            }
            else if (loc1 == 2 || loc1 == 3) 
            {
                if (this._storageGold < this.goldOutText + loc2) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(27957));
                }
                else 
                {
                    this.goldOutText = this.goldOutText + loc2;
                }
            }
            return;
        }

        /* internal */ /* var */_tabBar: GTabBar;

        /* internal */ /* var */_goodsList: GTileList;

        /* internal */ /* var */_goldTextBox: TextBox;

        /* internal */ /* var */_coinTextBox: TextBox;

        /* internal */ /* var */_coinOutText: GTextFiled;

        /* internal */ /* var */_bgItemSp: Object /* flash.display.Sprite */;

        /* internal */ /* var */_itemsData: Array<any>;

        /* internal */ /* var */_allocationGoodsData: Array<any>;

        /* internal */ /* var */_takeOutMoneyWindow: TakeOutMoneyWindow;

        /* internal */ /* var */_allocationConfirmWindow: AllocationConfirmWindow;

        /* internal */ /* var */_replyListWindow: ReplyListWindow;

        /* internal */ /* var */_storageGold: int;

        /* internal */ /* var */_storageCoin: int;

        /* internal */ /* var */_curSelectMemberData: SGuildPlayerAttendanceVo;

        /* internal */ /* var */_tabData: Array<any>;

        /* internal */ /* var */_goldOutText: GTextFiled;
    }
